package com.ims.core.doc.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.domain.TInCheckinInvoiceEntry;
import com.ims.core.doc.mapper.TInCheckinInvoiceMapper;
import com.ims.core.doc.domain.TInCheckinInvoice;
import com.ims.core.doc.service.ITInCheckinInvoiceService;

/**
 * 签收增值税发票Service业务层处理
 *
 * @author IMS
 * @date 2024-07-24
 */
@Service
public class TInCheckinInvoiceServiceImpl implements ITInCheckinInvoiceService
{
    @Autowired
    private TInCheckinInvoiceMapper tInCheckinInvoiceMapper;

    /**
     * 查询签收增值税发票
     *
     * @param id 签收增值税发票主键
     * @return 签收增值税发票
     */
    @Override
    public TInCheckinInvoice selectTInCheckinInvoiceById(Long id)
    {
        return tInCheckinInvoiceMapper.selectTInCheckinInvoiceById(id);
    }

    /**
     * 查询签收增值税发票列表
     *
     * @param tInCheckinInvoice 签收增值税发票
     * @return 签收增值税发票
     */
    @Override
    public List<TInCheckinInvoice> selectTInCheckinInvoiceList(TInCheckinInvoice tInCheckinInvoice)
    {
        return tInCheckinInvoiceMapper.selectTInCheckinInvoiceList(tInCheckinInvoice);
    }

    /**
     * 新增签收增值税发票
     *
     * @param tInCheckinInvoice 签收增值税发票
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTInCheckinInvoice(TInCheckinInvoice tInCheckinInvoice)
    {
        tInCheckinInvoice.setCreateTime(DateUtils.getNowDate());
        int rows = tInCheckinInvoiceMapper.insertTInCheckinInvoice(tInCheckinInvoice);
        insertTInCheckinInvoiceEntry(tInCheckinInvoice);
        return rows;
    }

    /**
     * 修改签收增值税发票
     *
     * @param tInCheckinInvoice 签收增值税发票
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInCheckinInvoice(TInCheckinInvoice tInCheckinInvoice)
    {
        tInCheckinInvoice.setUpdateTime(DateUtils.getNowDate());
        tInCheckinInvoiceMapper.deleteTInCheckinInvoiceEntryByInvoiceId(tInCheckinInvoice.getId());
        insertTInCheckinInvoiceEntry(tInCheckinInvoice);
        return tInCheckinInvoiceMapper.updateTInCheckinInvoice(tInCheckinInvoice);
    }

    /**
     * 批量删除签收增值税发票
     *
     * @param ids 需要删除的签收增值税发票主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInCheckinInvoiceByIds(Long[] ids)
    {
        tInCheckinInvoiceMapper.deleteTInCheckinInvoiceEntryByInvoiceIds(ids);
        return tInCheckinInvoiceMapper.deleteTInCheckinInvoiceByIds(ids);
    }

    /**
     * 删除签收增值税发票信息
     *
     * @param id 签收增值税发票主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInCheckinInvoiceById(Long id)
    {
        tInCheckinInvoiceMapper.deleteTInCheckinInvoiceEntryByInvoiceId(id);
        return tInCheckinInvoiceMapper.deleteTInCheckinInvoiceById(id);
    }

    /**
     * 新增签收增值税发票明细项信息
     *
     * @param tInCheckinInvoice 签收增值税发票对象
     */
    public void insertTInCheckinInvoiceEntry(TInCheckinInvoice tInCheckinInvoice)
    {
        List<TInCheckinInvoiceEntry> tInCheckinInvoiceEntryList = tInCheckinInvoice.getTInCheckinInvoiceEntryList();
        Long id = tInCheckinInvoice.getId();
        if (StringUtils.isNotNull(tInCheckinInvoiceEntryList))
        {
            List<TInCheckinInvoiceEntry> list = new ArrayList<TInCheckinInvoiceEntry>();
            for (TInCheckinInvoiceEntry tInCheckinInvoiceEntry : tInCheckinInvoiceEntryList)
            {
                tInCheckinInvoiceEntry.setInvoiceId(id);
                list.add(tInCheckinInvoiceEntry);
            }
            if (list.size() > 0)
            {
                tInCheckinInvoiceMapper.batchTInCheckinInvoiceEntry(list);
            }
        }
    }
}
